var logStore = Ext.create('Ext.data.Store', {
    fields: ["id", "content", "create_date"],
    pageSize: 15,
    proxy: {
        type: 'ajax',
        url: 'log/list',
        reader: {
            type: 'json',
            root: 'data'
        }
    },
    autoLoad: true
});
var logGrid = Ext.create("Ext.grid.Panel", {
    columnLines: true,
    frame: true,
    selType: 'checkboxmodel', // 设置选择模式 
    multiSelect: true, // 允许多选
    forceFit: true,
    columns: [
        {header: '主键', dataIndex: 'id', hidden: true},
        {header: '内容', dataIndex: 'content', width: 120},
        {header: '日期', dataIndex: 'create_date', width: 100}
    ],
    store: logStore,
    bbar: Ext.create('Ext.PagingToolbar', {
        store: logStore,
        displayInfo: true
    })
});

var logWin = Ext.create("Ext.window.Window", {
    title: "日志管理",
    //maximized: true,
    modal: true,
    width: 800,
    height: 400,
    maximizable: true,
    closeAction: "hide",
    layout: "fit",
    items: [logGrid]
});

var userStore = Ext.create('Ext.data.Store', {
    fields: ["id", "name", "admin", "emp_name","orgName"],
    pageSize: 15,
    proxy: {
        type: 'ajax',
        url: 'user/list',
        reader: {
            type: 'json',
            root: 'data'
        }
    },
    autoLoad: false
});
var userGrid = Ext.create("Ext.grid.Panel", {
    columnLines: true,
    frame: true,
    selType: 'checkboxmodel', // 设置选择模式 
    multiSelect: true, // 允许多选
    forceFit: true,
    columns: [
        {header: '主键', dataIndex: 'id', hidden: true},
        {header: '用户名', dataIndex: 'name', width: 120},
        {header: '类别', dataIndex: 'admin', width: 100, renderer: function(value) {
                if (value) {
                    return "<span style='color:red;'>管理员</span>";
                } else {
                    return "普通用户";
                }
            }},
        {header: '安全职员', dataIndex: 'emp_name', width: 100},
        {header: '所属机构', dataIndex: 'orgName', width: 100}
    ],
    store: userStore,
    tbar: [
        {xtype: "button", text: "删除", iconCls: "remove",
            handler: function() {
                Ext.Msg.confirm("提示", "删除不可恢复，您确定删除吗？", function(id) {
                    if (id === "yes") {
                        var mask = new Ext.LoadMask(userGrid, {msg: "正在删除..."});
                        mask.show();
                        var sel = userGrid.getSelectionModel().getSelection();
                        var ids = [];
                        for (i = 0; i < sel.length; i++) {
                            ids.push(sel[i].data.id);
                        }
                        Ext.Ajax.request({
                            url: "user/delete",
                            params: {
                                ids: ids
                            },
                            success: function(response) {
                                var json = Ext.JSON.decode(response.responseText);
                                Ext.Msg.alert("提示", json.msg, function() {
                                    userGrid.getStore().remove(sel);
                                    mask.hide();
                                });
                            },
                            failure: function(response) {
                                var json = Ext.JSON.decode(response.responseText);
                                Ext.Msg.alert("提示", json.msg, function() {
                                    mask.hide();
                                });
                            }
                        });
                    }
                });
            }
        }
    ],
    bbar: Ext.create('Ext.PagingToolbar', {
        store: userStore,
        displayInfo: true
    })
});

var userWin = Ext.create("Ext.window.Window", {
    title: "用户管理",
//    maximized: true,
    width: 800,
    height: 400,
    modal: true,
    maximizable: true,
    closeAction: "hide",
    layout: "fit",
    items: [userGrid]
});


Ext.define("Safe.view.BottomBar", {
    extend: 'Ext.toolbar.Toolbar',
    alias: 'widget.bottombar',
    initComponent: function() {
        var me = this;

        Ext.applyIf(me, {
            items: [
                {
                    iconCls: "config",
                    text: '系统',
                    menu: new Ext.menu.Menu({
                        id: 'mainMenu',
                        style: {
                            overflow: 'visible'     // For the Combo popup
                        },
                        items: [
                            {
                                text: "账户管理",
                                icon: "resource/images/ico/man.png",
                                handler: function() {
                                    userWin.show();
                                    userStore.load();
                                }
                            }, "-", {
                                text: "日志管理",
                                icon: "resource/images/ico/rule.png",
                                handler: function() {
                                    logWin.show();
                                }
                            }, "-", {
                                text: "切换主题",
                                menu: {
                                    items: [{
                                            text: "经典主题",
                                            handler: function() {
                                                Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
                                                Ext.state.Manager.set("theme", "resource/js/extjs4.2/resources/ext-theme-classic/ext-theme-classic-all.css");
                                                document.location.reload();
                                            }
                                        }, {
                                            text: "海王星主题",
                                            handler: function() {
                                                Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
                                                Ext.state.Manager.set("theme", "resource/js/extjs4.2/resources/ext-theme-neptune/ext-theme-neptune-all.css");
                                                document.location.reload();
                                            }
                                        }, {
                                            text: "灰色主题",
                                            handler: function() {
                                                Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
                                                Ext.state.Manager.set("theme", "resource/js/extjs4.2/resources/ext-theme-gray/ext-theme-gray-all.css");
                                                document.location.reload();
                                            }
                                        }, {
                                            text: "黑色主题",
                                            handler: function() {
                                                Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
                                                Ext.state.Manager.set("theme", "resource/js/extjs4.2/resources/ext-theme-access/ext-theme-access-all.css");
                                                document.location.reload();
                                            }
                                        }, {
                                            text: "经典沙盒主题",
                                            handler: function() {
                                                Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
                                                Ext.state.Manager.set("theme", "resource/js/extjs4.2/resources/ext-theme-classic-sandbox/ext-theme-classic-sandbox-all.css");
                                                document.location.reload();
                                            }
                                        }]
                                }
                            }
                        ]
                    })
                }
            ]
        });

        me.callParent(arguments);
    }
});


